<template>
  <div class="search">
    <el-form ref="queryFormRef" :model="address" :inline="true">
      <el-form-item label="关键字" prop="name">
        <el-input v-model="address" placeholder="教室名称" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getAllInfo"><i-ep-search />搜索</el-button>
        <el-button @click="resetQuery">
          <i-ep-refresh />
          重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-card>
      <el-button type="primary" round @click="addRoomInfo">添加教室</el-button>
    </el-card>
    <el-table :data="classroomData" style="width: 100%">
      <el-table-column prop="id" label="教室编号" width="500" />
      <el-table-column prop="address" label="空教室" width="500" />
      <el-table-column fixed="right" label="操作" width="250">
        <template #default="scoped">
          <el-button type="danger" round @click="deleteClassroom(scoped.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页搜索 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="pageTotal"
      @size-change="pageChange"
      @current-change="currentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </div>

  <!--添加教室信息 -->
  <el-dialog v-model="dialogAdd" title="添加空教室">
    <el-form :model="roomAdd">
      <el-form-item label="教室位置">
        <el-input v-model="roomAdd.address" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="info" round @click="dialogAdd = false">取消</el-button>
        <el-button type="success" round @click="addDialog"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { onMounted, ref, reactive } from "vue";
import { ElMessageBox } from "element-plus";
import { deleteRoom, getAllRoom, addRoom } from "@/api/classroom.js";

const classroomData = ref([]);

const dialogAdd = ref(false);

const roomAdd = reactive({
  address: "",
});

const currentPage = ref(1);
const pageSize = ref(3);
const address = ref("");
//总共页数
const pageTotal = ref(0);

//页数改变时
const pageChange = async (size) => {
  pageSize.value = size;
  getAllInfo();
};

//页数大小改变时
const currentChange = async (num) => {
  currentPage.value = num;
  console.log(10086);
  console.log(num);
  getAllInfo();
};

//重置查询
const resetQuery = () => {
  currentPage.value = 1;
  pageSize.value = 3;
  console.log(currentPage.value);
  address.value = "";
  getAllInfo();
};
//添加弹框
const addRoomInfo = async () => {
  dialogAdd.value = true;
};

//添加课程信息
const addDialog = async () => {
  let result = await addRoom(roomAdd);
  ElMessage.success(result.message ? result.message : "添加成功");

  getAllInfo();
  dialogAdd.value = false;
};

//删除教师信息
const deleteClassroom = async (id) => {
  ElMessageBox.confirm("你确认要删除教室信息吗?", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      //调用接口
      let result = await deleteRoom(id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      //刷新列表
      getAllInfo();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除",
      });
    });
};

//获取所有信息
const getAllInfo = async () => {
  let params = {
    page: currentPage.value,
    pageSize: pageSize.value,
    address: address.value ? address.value : null,
  };
  let result = await getAllRoom(params);
  console.log(result.data);
  classroomData.value = result.data.records;
  pageTotal.value = result.data.total;
};

onMounted(() => {
  getAllInfo();
});
</script>
